package com.qfedu.controller;

import com.qfedu.common.JsonResult;
import com.qfedu.entity.User;
import com.qfedu.service.UserService;
import com.qfedu.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * projectName: bankSystem
 * author: CGY
 * time: 2020/9/9 16:28
 * description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 查询用户信息
     * @param session
     * @return
     */
    @RequestMapping("/query.do")
    @ResponseBody

    public JsonResult getLoginInfo(HttpSession session){
        User user = (User) session.getAttribute(StrUtils.LOGIN_USER);
        //需要再次从数据库查一遍
         user = userService.selectByCode(user.getBankCode());

        JsonResult jsonResult = null;
        if (user != null){
            jsonResult = new JsonResult(1, user);
        }else {
            jsonResult = new JsonResult(0, "未登录");
        }

        return jsonResult;
    }

    /***
     * 修改用户密码
     * @param oldPassword
     * @param newPassword
     * @param session
     * @return
     */
    @RequestMapping("/changepassword.do")
    @ResponseBody
    public JsonResult changepassword(String oldPassword,String newPassword,HttpSession session){

        User user = (User) session.getAttribute(StrUtils.LOGIN_USER);
        userService.updatePassword(user.getBankCode(),oldPassword,newPassword);

        JsonResult jsonResult = new JsonResult(1, "修改成功");

        return jsonResult;
    }

    /**
     * 上传图片
     * @param session
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/logout.do")
    @ResponseBody
    public void logOut(HttpSession session, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        //清除session
        session.removeAttribute(StrUtils.LOGIN_USER);
        //验证转发 退出到登陆页面出现乱码问题，里面的其他静态资源无法被获取 较特殊
       // httpServletRequest.getRequestDispatcher("/login.html").forward(httpServletRequest,httpServletResponse);
        //验证重定向没有问题
        httpServletResponse.sendRedirect("/login.html");
    }

    /**
     * 注册账号
     * @param bankCode
     * @param password
     * @return
     */
    @RequestMapping("/register.do")
    @ResponseBody
    public JsonResult register(String bankCode,String password){

        User user = new User();
        user.setBankCode(bankCode);
        user.setPassword(password);

        return userService.insertUser(user);
    }
}
